package com.xyh.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.xyh.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000121698734";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCuHbfUvkYx7NNbyw9CZ6NlCjM7pkzFfhiro18h9zPa2n1hMHj85UCqBnX8dlMlbO5b631YZBP2WswXyGnMBzBQRdUGmfRRd8I2tn+9pcFAAIvX1K3Jd/2iC4/3I1REWLOKhbzicTQiLINV3AkKgHJM23rrEi6yoZB5/B6uEuTwEkSK3ZQT5/gccBweviT9k5GuDVTBo95j20UJGAik108Lhs5KRv9AX8/JHdEWy7rkefWFqnw1on311sMocvObCyKpI7raPYTkiDRyODB29HQFqbImRv7OHKpoXy/lwuh2nvSmGMryfYaaeMQ8WT3gym510wo5EfHTmbis7W1Uvq3DAgMBAAECggEAJvw8dwQLj44pXPpAdmviJ/N6uTZpJicvuu6flGlXUP9u514FGQrzyZ4cuOR9hXm4fRhjPnFaDuURoZmcJrTNWovoQMwj6kHYWIvQWlRMytnUdgUGzH9zaN8R4xIkAhGiUbfULRePxr8UwRq9Y36TGSZjhVLS5ehLAyUycWOFo0L1Q3GQ6WSWsS44mrv+UgOgPo0GBXmaFtNR3ogBIoztLIooEhL4gSem+3IHqI+TJRHfuIlIKek9ekpGhUewZJDWjXSCcvE1jbcrtsgMli2TOsffMEfau+GNOgfVkQPHa0JB/CJl4h68+XoGkvxoHgJ9NovorYn38185tqGFmegBoQKBgQDZlPRoWOIak+w07avdhvsGl6knr7x8g5vFVvGS7B9T/53HZKTI2CQuFJjPKhUYX1jWv8w5CKWbjxlnC323JY1BgEY//w+N3QjqAawLxR6ppyD2renJjGwHEq274HR05AizS7TU4BIKXPS/GTWnjKMZbv9sNu1iuoXleCi8Hito6wKBgQDM3AqpHUlYJYL/6cb/3Y/iu3EKhL0i2qz6B1KsYxfv/sSN9WLxoi9qzSuYiqV5KYVij2t06RBvMoLeBcBYnLA+WaAJkpN0wzyNkm5tOv3JYtdp2x/H8hjf+9QkfOQ71SGFYoxme9oAwUi0EPpuRA7aDOnAhp13qYsQ08hr7aiYiQKBgG72v1rEEbO6QtmPaHoWz8jB6du6jSb3O6b9uz/RufxtvXqmAkAHsxPpHzFv5chDQBxtPmrL1SGrbioWOyv/M7X3BFy46KbR1iz5Y1OxWPzupioH8dX/DkzhIsdRJjNC5IkEEBoMmEa+t3cKFUn4uGlNKWh37sLOG2E20Zd2z263AoGBAL1LvpYSN8m5r9jlnjqj8ubhqUy850AjrJ7E0YH1dxMO6XT7gFgjM4mO2G/1/X19/oDaYcL/8W2u0Ppzeg7ninuyuYze4l6MYZG3803/qq7CJKbuEeniLVYbDyZAOW2IPZdRPsccPo9gCtxIq//j541ZTU3Xlf4Sriv4ZkREsPtxAoGAXMoxdRk66UkXI9+pdt02BFqSQB12tz85TJe7LHnquZHoxfBSVn6iMac20LvqGoOa1RcCch3isc9Dd1sy5leL3F4bdEDh0zd5YsNzhWBZUhCfraRXIHIXPxtF+dZUxOLZFzFANCkIIruw5qxDPf9HzyK/4bDunyC4nqVPy1APV2A=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApI1wgVe2F94ejEDZBxIyg7326uQ/7U8A6xGRmPqnQiOuVPmQE46/dEqhdbukOwfLAiJhei6wI55coDx/8A9cef41RJmW4r5oMuvGHOi0j8XQAZxSNfGxtYk/Pe7in3f/06wj6iu3IgjxpSqGVCaP0bc9HgQ/s+H/pBuBCMmlsm6VVWaq+ZN5rxnMVj0k3DnPhZcrWdVPFkD8dJ8MPZC03vi7mPHqaFropdHy8W+mp+9n+0iXkjrAmg7unO0xV+uvQRgCVk80YMWaUbTDOdOEKceK3tGHpFhqg6aoN7fOVrrNc60t6a4A0ksExoBADkZVhz6yQafO2LvHQhL57XNM3QIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://672j3j6946.goho.co/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    private String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
